<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片试图 -->
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="showAddCateDialog">添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <tree-table class="treeTable" :data="catelist" :columns="columns" :selection-type="false" :expand-type="false" show-index border :show-row-hover="false">
        <!-- 是否有效 -->
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-success" v-if="scope.row.cat_deleted===false" style="color:lightgreen"></i>
          <i class="el-icon-error" v-else style="color:red"></i>
        </template>
        <!-- 排序 -->
        <template slot="order" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level === 0 " type="primary">一级</el-tag>
          <el-tag v-else-if="scope.row.cat_level === 1" type="success">二级</el-tag>
          <el-tag v-else type="warning">三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="opt" slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" @click="showEditDialog(scope.row)">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" @click="removeCateById(scope.row.cat_id)">删除</el-button>
        </template>
      </tree-table>
      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum" :page-sizes="[5, 7, 13, 20]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total" style="text-align:center">
      </el-pagination>
    </el-card>

    <!-- 添加分类的对话框 -->
    <el-dialog title="添加分类" :visible.sync="addCatedialogVisible" width="40%" @close="addCatedialogClosed">
      <el-form :model="addCateForm " ref="addCateFormRef">
        <el-form-item label="父级分类:" prop="cat_name">
          <el-input v-model="addCateForm.cat_name" style="width:80%"></el-input>
        </el-form-item>
        <el-form-item label="父级分类:">
          <el-cascader v-model="selectedKeys" :options="parentCateList" :props="cascaderProps" @change="parentCateChange" clearable  change-on-select></el-cascader>
        </el-form-item>
      </el-form>
      <span>
        <el-button @click="addCatedialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 编辑分类对话框 -->
    <el-dialog title="编辑分类" :visible.sync="editCatedialogVisible" width="40%" @close="editCatedialogClosed">
      <el-form :model="editCateForm " ref="editCateFormRef">
        <el-form-item label="分类id:" prop="cat_id">
          <el-input v-model="editCateForm.cat_id" style="width:80%"></el-input>
        </el-form-item>
        <el-form-item label="分类名称:" prop="cat_name">
          <el-input v-model="editCateForm.cat_name" style="width:80%"></el-input>
        </el-form-item>
      </el-form>
      <span>
        <el-button @click="editCatedialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      catelist: [],
      // 查询条件
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      // 分页
      total: 0,
      // 为table指定列的定义
      columns: [
        { label: '分类名称', prop: 'cat_name' },
        { label: '是否有效', type: 'template', template: 'isok' },
        { label: '排序', type: 'template', template: 'order' },
        { label: '操作', type: 'template', template: 'opt' }
      ],
      // 显示添加分类对话框
      addCatedialogVisible: false,
      // 对话框数据
      addCateForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      // 父级分类列表
      parentCateList: [],
      // 级联表配置
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        expandTrigger: 'hover'
      },
      // 选择父级分类的id数据
      selectedKeys: [],
      // 编辑分类对话框
      editCatedialogVisible: false,
      editCateForm: {
        cat_id: 0,
        cat_name: ''
      }
    }
  },
  created () {
    this.getCateList()
  },
  methods: {
    // 分页
    handleSizeChange (val) {
      this.queryInfo.pagesize = val
      this.getCateList()
    },
    handleCurrentChange (val) {
      this.queryInfo.pagenum = val
      this.getCateList()
    },
    // 获取商品列表
    async getCateList () {
      const { data: res } = await this.$http.get('categories', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error('请求失败')
      this.catelist = res.data.result
      this.total = res.data.total
      // console.log(res.data)
    },
    // 显示添加分类的对话框
    showAddCateDialog () {
      this.getParentCateList()
      this.addCatedialogVisible = true
    },
    // 获取父级分类
    async getParentCateList () {
      const { data: res } = await this.$http.get('categories', { params: { type: 2 } })
      if (res.meta.status !== 200) return this.$message.error('请求失败')
      this.parentCateList = res.data
      // console.log(res.data)
    },
    // 级联表变化
    parentCateChange () {
      console.log(this.selectedKeys)
      if (this.selectedKeys.length > 0) {
        this.addCateForm.cat_pid = this.selectedKeys[this.selectedKeys.length - 1]
        this.addCateForm.cat_level = this.selectedKeys.length
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      }
      // console.log(this.addCateForm)
    },
    // 添加商品分类
    async addCate () {
      const { data: res } = await this.$http.post('categories', this.addCateForm)
      if (res.meta.status !== 201) return this.$message.error('添加失败')
      this.$message.success('添加成功')
      this.getCateList()
      this.addCatedialogVisible = false
    },
    // 清空对话框
    addCatedialogClosed () {
      this.$refs.addCateFormRef.resetFields()
      this.selectedKeys = {}
      this.addCateForm.cat_level = 0
    },
    showEditDialog (row) {
      this.editCatedialogVisible = true
      this.editCateForm.cat_id = row.cat_id
      this.editCateForm.cat_name = row.cat_name
      console.log(row)
    },
    // 修改对话框关闭
    editCatedialogClosed () {
      this.editCatedialogVisible = false
    },
    // 修改
    async editCate () {
      console.log(this.editCateForm)
      const { data: res } = await this.$http.put(`categories/${this.editCateForm.cat_id}`, { id: this.editCateForm.cat_id, cat_name: this.editCateForm.cat_name })
      // console.log(res.data)
      if (res.meta.status !== 200) {
        return this.$message.error('修改失败')
      }
      this.$message.success('成功')
      this.editCatedialogVisible = false
      this.getCateList()
    },
    async removeCateById (id) {
      const confirm = await this.$confirm('确认删除分类吗', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch((err) => { console.log(err) })
      if (confirm !== 'confirm') { return this.$message.info('已取消') }
      const { data: res } = await this.$http.delete(`categories/${id}`)
      if (res.meta.status !== 200) {
        return this.$message.error('删除失败')
      }
      this.$message.success('成功')
      this.getCateList()
    }

  }
}
</script>

<style lang="less" scoped>
.treeTable {
  margin-top: 15px;
}
.el-cascader{width: 80%;}
</style>
